﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace HMS_Hospital_Team2._6.DA
{
    class PhongBanDA
    {
        // DA
        HMSHospitalEntities context = new HMSHospitalEntities();
        PubicLibDA pubPB = new PubicLibDA();

        // Hàm load Data lên gridview
        public DataTable LoadthongtinPB_DA()
        {
            var PhongBan = from Pb in context.PhongBans
                           select new
                           {
                               Pb.MaPhongBan,
                               Pb.TenPhongBan,
                               Pb.NhomDichVu.TenNhomDV,
                               Pb.GhiChu,
                               Pb.TrangThai
                           };
            return pubPB.LinqToDataTable(PhongBan);
        }
        // Hàm load 
        public DataTable LoadPhongBan_DA()
        {
            var PhongBan = from Pb in context.PhongBans
                           where Pb.MaPhongBan.Contains("TN")
                           select new
                           {
                               Pb.MaPhongBan,
                               Pb.TenPhongBan,
                               Pb.NhomDichVu.TenNhomDV,
                               Pb.GhiChu,
                               Pb.TrangThai
                           };
            return pubPB.LinqToDataTable(PhongBan);
        }
        // Hàm load thông tin bàn thu
        public DataTable Loadthongtinbanthu_DA(string sMaphongBan)
        {
            var banthu = from bt in context.BanThus
                         where bt.MaPhongBan == sMaphongBan
                         select new
                         {
                             bt.TenBan,
                             bt.MaBan
                         };
            return pubPB.LinqToDataTable(banthu);
        }
        // Hàm load Data tên loại PB lên Combobox
        public DataTable LoadthongtinTenlPB_DA()
        {
            var Tlpb = from lpb in context.LoaiPhongBans
                       select new
                       {
                           lpb.TenLoaiPhong,
                           lpb.MaLoaiPhong
                       };
            return pubPB.LinqToDataTable(Tlpb);
        }
        // Hàm load Data tên nhóm DV lên Combobox
        public DataTable LoadthongtinTennDV_DA()
        {
            var Tndv = from ndv in context.NhomDichVus
                       select new
                       {
                           ndv.TenNhomDV,
                           ndv.MaNhomDV
                       };
            return pubPB.LinqToDataTable(Tndv);
        }
        // Hàm Sửa phòng ban
        public static void ChinhsuaPB_DA(string sMaPB, string MaLpb, string TenPb, string MaNdv, string Ghichu, bool Trangthai)
        {
            using (HMSHospitalEntities Sua = new HMSHospitalEntities())
            {
                var Phongban = (from pb in Sua.PhongBans
                                where pb.MaPhongBan == sMaPB
                                select pb).First();
                Phongban.MaPhongBan = sMaPB;
                Phongban.MaLoaiPhong = MaLpb;
                Phongban.TenPhongBan = TenPb;
                Phongban.MaNhomDV = MaNdv;
                Phongban.GhiChu = Ghichu;
                Phongban.TrangThai = Trangthai;
                Sua.SaveChanges();

            }
        }
        //Hàm xử lý bắt lỗi kiểm tra khi bị trùng dữ liệu
        public static bool CheckdataPB_DA(string Tenpb)
        {
            using (HMSHospitalEntities dulieutrung = new HMSHospitalEntities())
            {
                bool chek = true;
                var phongban = (from pb in dulieutrung.PhongBans
                              select pb);
                foreach (var row in phongban)
                {
                    if (row.TenPhongBan == Tenpb)// Kiểm tra Tên phòng ban có trong database
                    {
                        chek = true;
                        break;
                    }
                    else
                    {
                        chek = false;
                    }
                }
                return chek;
            }

        }
    }
}
